package com.secneo.appshare.threads;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.secneo.appshare.R;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.common.Util;
import com.secneo.appshare.remind.Remind;
import com.secneo.appshare.remind.Remind.RemindAction;

public class RemindThread extends Thread{
	
	Handler mHandler = null ;
	List<Remind> remindList = null ;
	Context mContext = null ;
	private int mActionType;
	private boolean running = false;
	
	public RemindThread(Handler handler , Context context, int actionType){
		mHandler = handler ;
		mContext = context ;
		mActionType = actionType;
	}
	
	public synchronized void stopRun()
	{
		running = false;
		this.interrupt();
	}
	
	private void sendMessage(int what){
		Message msg = new Message() ;
		msg.what = what ;
		mHandler.sendMessage(msg) ;
	}
	
	public List<Remind> getList(){
		return remindList ;
	}
	
	public void run()
	{
		running = true;
		RemoteApiImpl api_impl = new RemoteApiImpl() ;

		remindList = api_impl.getRemindList(mContext , LocalStore.getLocaleStoreInstance(mContext).getUid(), mActionType);
		
		if(remindList == null)
		{
			sendMessage(Constants.GET_REMIND_ERR) ;
			return;
		}
		
		if(remindList.size() == 0)
		{
			sendMessage(Constants.GET_REMIND_NO_DATA) ;
			return;
		}
		sendMessage(Constants.GET_REMIND_FINISH) ;
		
		for(int i = 0 ; i < remindList.size() && running; ++i)
		{
			Remind remind = remindList.get(i) ;
			if(remind.getUserIcon() == null)
			{
				//如果还没有设置过图片
				String iconUrl = remind.getUserIconUrl();
				if(iconUrl.contains("buildin"))
				{
					try 
					{
						remind.setUserIcon(Util.getDrawableFromAsset(mContext, iconUrl));
						
					} catch (IOException e) {
						e.printStackTrace();
					}
				} 
				else 
				{
					try 
					{
						Drawable d_tmp = Util.getDrawableFromCache(mContext, iconUrl);
						remind.setUserIcon(d_tmp) ;
						
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			
			if(remind.getAction() == RemindAction.NEWSHAREACTION ||
					remind.getAction() == RemindAction.NEWCOLLECTIONACTION ||
					remind.getAction() == RemindAction.NEWCOMMENTACTION ||
					remind.getAction() == RemindAction.NEWDOWNLOADACTION)
			{
				if(remind.getAppIcon() == null)
				{	
					//如果还没有设置过app图片
					String appUrl = remind.getAppIconUrl();
					Drawable d_tmp = null;
					try
					{
						d_tmp = Util.getDrawableFromCache(mContext, appUrl);
					} catch (IOException e)
					{
						e.printStackTrace();
					}
					remind.setAppIcon(d_tmp) ;
				}
			}
			sendMessage(Constants.GET_REMIND_ICON_FINISH) ;
		}
	}
	
}
